package online.foxnull2.foxmall.coupon.controller;

import online.foxnull2.common.utils.R;
import online.foxnull2.foxmall.coupon.entity.SeckillSessionEntity;
import online.foxnull2.foxmall.coupon.entity.SeckillSkuRelationEntity;
import online.foxnull2.foxmall.coupon.service.SeckillSessionService;
import online.foxnull2.foxmall.coupon.service.SeckillSkuRelationService;
import online.foxnull2.foxmall.coupon.utils.PageUtils;
import online.foxnull2.foxmall.coupon.vo.seckillSession.SeckillSessionVo;
import online.foxnull2.foxmall.coupon.vo.seckillSession.SeckillSkuRelation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 秒杀活动场次
 *
 * @author foxnull2
 */
@RestController
@RequestMapping("coupon/seckillsession")
public class SeckillSessionController {

    private final SeckillSessionService seckillSessionService;
    private final SeckillSkuRelationService seckillSkuRelationService;

    @Autowired
    public SeckillSessionController(SeckillSessionService seckillSessionService,
                                    SeckillSkuRelationService seckillSkuRelationService) {
        this.seckillSessionService = seckillSessionService;
        this.seckillSkuRelationService = seckillSkuRelationService;
    }

    @GetMapping("/list/seckillSession/nextThreeDays")
    public List<SeckillSessionVo> listSeckillSessionWithNextThreeDays() {
        List<SeckillSessionEntity> seckillSessionEntities = seckillSessionService.listSeckillSessionWithNextThreeDays();
        if (!ObjectUtils.isEmpty(seckillSessionEntities)) {
            return seckillSessionEntities.stream().map(seckillSessionEntity -> {
                Long promotionSessionId = seckillSessionEntity.getId();
                SeckillSessionVo seckillSessionVo = new SeckillSessionVo();
                seckillSessionVo.setId(promotionSessionId)
                        .setName(seckillSessionEntity.getName())
                        .setStartTime(seckillSessionEntity.getStartTime())
                        .setEndTime(seckillSessionEntity.getEndTime())
                        .setStatus(seckillSessionEntity.getStatus())
                        .setCreateTime(seckillSessionEntity.getCreateTime());
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities = seckillSkuRelationService.listSeckillSkuRelationByPromotionSessionId(promotionSessionId);
                if (!ObjectUtils.isEmpty(seckillSkuRelationEntities)) {
                    List<SeckillSkuRelation> seckillSkuRelations = seckillSkuRelationEntities.stream().map(seckillSkuRelationEntity -> {
                        SeckillSkuRelation seckillSkuRelation = new SeckillSkuRelation();
                        seckillSkuRelation.setId(seckillSkuRelationEntity.getId())
                                .setPromotionId(seckillSkuRelationEntity.getPromotionId())
                                .setPromotionSessionId(seckillSkuRelationEntity.getPromotionSessionId())
                                .setSkuId(seckillSkuRelationEntity.getSkuId())
                                .setSeckillPrice(seckillSkuRelationEntity.getSeckillPrice())
                                .setSeckillCount(seckillSkuRelationEntity.getSeckillCount())
                                .setSeckillLimit(seckillSkuRelationEntity.getSeckillLimit())
                                .setSeckillSort(seckillSkuRelationEntity.getSeckillSort());
                        return seckillSkuRelation;
                    }).collect(Collectors.toList());
                    seckillSessionVo.setSeckillSkuRelations(seckillSkuRelations);
                }
                return seckillSessionVo;
            }).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = seckillSessionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SeckillSessionEntity seckillSession = seckillSessionService.getById(id);

        return R.ok().put("seckillSession", seckillSession);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SeckillSessionEntity seckillSession) {
        seckillSession.setCreateTime(new Date());
        seckillSessionService.save(seckillSession);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SeckillSessionEntity seckillSession) {
        seckillSessionService.updateById(seckillSession);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        seckillSessionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
